package com.example.hotel;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.hotel.Add_cart.AdapterListViewData;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Billing_history extends Activity implements OnClickListener {
	public ArrayList<String> message = new ArrayList<String>();
	HashMap<String, String> planetList1;
	SimpleAdapter sAdap;
	Dialog bill;

	ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
	TextView history, date, wellcome, tv_total_price, tv_Product_name, tv_Type,
			tv_Price, tv_QTY, tv_Total, tv_Update, tv_Remove, tv_Total_Payment,
			tv_billNo, tv_customerName, tv_total, tv_date, tv_view,
			history_bill, tv_bill;
	ImageView back_home, imageback, image_cart, image_msg, image_bill;
	final ArrayList<String> bill_No = new ArrayList<String>();
	final ArrayList<String> customer_name = new ArrayList<String>();
	final ArrayList<String> total = new ArrayList<String>();
	final ArrayList<String> date_time = new ArrayList<String>();
	final ArrayList<String> show_bill = new ArrayList<String>();
	Double total_price = 0.00;
	WebView webview;
	ArrayList<HashMap<String, String>> myArrList;
	final Context context = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.billing_history);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}

		TextView tvAmount = (TextView) findViewById(R.id.tvAmount);
		tvAmount.setText("("
				+ ((Global) getApplicationContext()).cart_count.toString()
				+ ")");
		getBilling();
		myArrList = new ArrayList<HashMap<String, String>>();
		tv_Total_Payment = (TextView) findViewById(R.id.tv_Total_Payment);
		tv_Product_name = (TextView) findViewById(R.id.tv_Product_name);
		tv_Type = (TextView) findViewById(R.id.tv_Type);

		tv_billNo = (TextView) findViewById(R.id.tv_billNo);
		tv_customerName = (TextView) findViewById(R.id.tv_customerName);
		tv_total = (TextView) findViewById(R.id.tv_total);
		tv_date = (TextView) findViewById(R.id.tv_date);
		tv_view = (TextView) findViewById(R.id.tv_view);
		history_bill = (TextView) findViewById(R.id.history_bill);
		tv_bill = (TextView) findViewById(R.id.bill);
		HashMap<String, String> map;

		for (int i = 0; i < bill_No.size(); i++) {
			map = new HashMap<String, String>();
			map.put("bill", bill_No.get(i));
			map.put("customer_name", customer_name.get(i));
			map.put("total", total.get(i));
			map.put("date_time", date_time.get(i));
			map.put("view_bill", show_bill.get(i));

			myArrList.add(map);
		}
		ListView listViewData = (ListView) findViewById(R.id.listView1);
		listViewData.setAdapter(new AdapterListViewData(this));
		image_cart = (ImageView) findViewById(R.id.image_cart);
		image_msg = (ImageView) findViewById(R.id.image_msg);
		image_bill = (ImageView) findViewById(R.id.image_bill);
		back_home = (ImageView) findViewById(R.id.imagehome);
		imageback = (ImageView) findViewById(R.id.imageback);
		image_msg.setOnClickListener(this);
		image_cart.setOnClickListener(this);
		image_bill.setOnClickListener(this);
		back_home.setOnClickListener(this);
		wellcome = (TextView) findViewById(R.id.tvWellcome);
		wellcome.setText(((Global) getApplicationContext()).wellcome);

		/*------setfonts------*/
		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		TextView tv_amount = (TextView) findViewById(R.id.tvAmount);
		tv_amount.setTypeface(Font);
		wellcome.setTypeface(FontBold);
		tv_billNo.setTypeface(FontBold);
		tv_customerName.setTypeface(FontBold);
		tv_total.setTypeface(FontBold);
		tv_date.setTypeface(FontBold);
		tv_view.setTypeface(FontBold);
		history_bill.setTypeface(FontBold);
		tv_bill.setTypeface(FontBold);
		Boolean flag_call = ((Global) getApplicationContext()).callpage_bool;
		if (flag_call) {
			imageback.setImageDrawable(getResources().getDrawable(
					R.drawable.arrow_upright));
			imageback.setOnClickListener(this);
		} else {
			((Global) getApplicationContext()).callpage_bool = false;
		}

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imagehome:
			startActivity(new Intent(Billing_history.this, home.class));
			break;
		case R.id.imageback:
			Class myclass = ((Global) getApplicationContext()).callpage1;
			startActivity(new Intent(Billing_history.this, myclass));
			break;
		case R.id.image_cart:
			((Global) getApplicationContext()).nameCart = "show_cart";
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Billing_history.class;
			startActivity(new Intent(Billing_history.this, Add_cart.class));
			break;
		case R.id.image_msg:
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Billing_history.class;
			startActivity(new Intent(Billing_history.this, Message_list.class));
			break;
		case R.id.image_bill:
			startActivity(new Intent(Billing_history.this,
					Billing_history.class));
			break;
		}
	}

	public class AdapterListViewData extends BaseAdapter {

		private LayoutInflater mInflater;
		private Context context; // เน€เธเธเน€เธเธ‘เน€เธ๏ฟฝ Context
									// เน€เธ๏ฟฝเน€เธเธ’เน€เธ๏ฟฝ

		public AdapterListViewData(Context c) {
			// TODO Auto-generated method stub
			context = c;
		} // CustomListViewActivity

		// DataShow

		public int getCount() {
			// TODO Auto-generated method stub
			return bill_No.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			final HolderListAdapter holderListAdapter;
			Typeface Font = Typeface.createFromAsset(getAssets(),
					"fonts/PSL-Kittithada.ttf");

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bill_column, null);
			}
			holderListAdapter = new HolderListAdapter();
			holderListAdapter.bill_No = (TextView) convertView
					.findViewById(R.id.bill_no);

			holderListAdapter.customer_name = (TextView) convertView
					.findViewById(R.id.customer_name);
			holderListAdapter.total = (TextView) convertView
					.findViewById(R.id.total);
			holderListAdapter.date_time = (TextView) convertView
					.findViewById(R.id.tv_date);
			holderListAdapter.view_bill = (ImageButton) convertView
					.findViewById(R.id.view_bill);

			convertView.setTag(holderListAdapter);

			holderListAdapter.bill_No.setText(myArrList.get(position).get(
					"bill"));
			holderListAdapter.bill_No.setTypeface(Font);
			holderListAdapter.customer_name.setText(myArrList.get(position)
					.get("customer_name"));
			holderListAdapter.customer_name.setTypeface(Font);
			holderListAdapter.total.setText(myArrList.get(position)
					.get("total"));
			holderListAdapter.total.setTypeface(Font);
			holderListAdapter.date_time.setText(myArrList.get(position).get(
					"date_time"));
			holderListAdapter.date_time.setTypeface(Font);

			holderListAdapter.view_bill
					.setOnClickListener(new OnClickListener() {

						public void onClick(View v) {

							bill = new Dialog(context);
							bill.setContentView(R.layout.show_bill);
							WebView myWebView = (WebView) bill
									.findViewById(R.id.bill);
							myWebView.loadUrl(myArrList.get(position).get(
									"view_bill"));
							bill.setTitle("Bill");
							bill.show();
						}
					});
			return convertView;
		}
	}

	// /CustomList setFontListView
	@SuppressWarnings("rawtypes")
	private class CustomListAdapter extends ArrayAdapter {

		private Context mContext;
		private int id;
		private List<String> items;

		@SuppressWarnings("unchecked")
		public CustomListAdapter(Context context, int textViewResourceId,
				List<String> list) {
			super(context, textViewResourceId, list);
			mContext = context;
			id = textViewResourceId;
			items = list;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			View mView = v;
			if (mView == null) {
				LayoutInflater vi = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				mView = vi.inflate(id, null);
			}
			TextView text = (TextView) mView.findViewById(R.id.message_list);
			if (items.get(position) != null) {
				Typeface Font = Typeface.createFromAsset(getAssets(),
						"fonts/PSL-Kittithada.ttf");
				text.setTypeface(Font);
				text.setText(items.get(position));
			}
			return mView;
		}

	}

	public void getBilling() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ "billing_history";
		String NAMESPACE = "";
		String METHOD_NAME = "billing_history";
		String lang = ((Global) getApplicationContext()).lang;
		String imei = ((Global) getApplicationContext()).imei;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			String str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			indexSub = b.indexOf(";");
			b = b.substring(0, indexSub);
			String[] a = b.split("\\#");
			for (int i = 0; i < a.length; i++) {
				String[] tmp = a[i].split("\\*");
				bill_No.add(tmp[0]);
				customer_name.add(tmp[1]);
				date_time.add(tmp[3]);
				show_bill.add(tmp[4]);
				Double p = Double.parseDouble(tmp[2]);
				total_price += p;
				DecimalFormat decimalFormat = new DecimalFormat("###,##0.00");
				total.add(decimalFormat.format(p));
			}

		} catch (Exception e) {
			// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}

}
